package com.jin.webvideo.util.entity;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jin.webvideo.base.util.CollectionsUtil;
import com.jin.webvideo.entity.Video;
import com.jin.webvideo.entity.VideoInfo;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.externalInterfaces.entity.FilmographyInfo;
import com.jin.webvideo.externalInterfaces.entity.Participation;
import com.jin.webvideo.externalInterfaces.entity.ParticipationMate;
import com.jin.webvideo.util.sys.EncryptUtil;
import com.jin.webvideo.util.base.StringUtil;
import com.jin.webvideo.util.base.URLUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class EntityUtil {

    /**
     * json序列化
     * @param obj
     * @param skipTransientField 是否不序列化transient标识
     * @return
     */
    public static String jsonSerializer(Object obj, boolean skipTransientField){
        SerializeWriter out = new SerializeWriter();
        JSONSerializer serializer = new JSONSerializer(out);
        serializer.config(SerializerFeature.SkipTransientField, skipTransientField);
        serializer.write(obj);
        String json = out.toString();
        out.close();
        return json;
    }

    /**
     * video 转化成 Filmography
     * @param video
     * @return
     */
    public static Filmography toFilmography(Video video){
        if (null == video){
            return null;
        }
        Filmography filmography = new Filmography();
        filmography.setDesc(video.getDescription());
        filmography.setName(video.getTitle());
        filmography.setYear(video.getYear());
        filmography.setId(video.getDocid());
        filmography.setPlatform(video.getPlatform());
        filmography.setPlatformName(PlatformSupport.platformName(video.getPlatform()));
        filmography.setImg(video.getImg());
        filmography.setTag(video.getTag());
        filmography.setDisplayMode(video.getDisplayMode());
        ParticipationMate part = new ParticipationMate();
        part.setActor(string2Part(video.getActor()));
        part.setDirector(string2Part(video.getDirector()));
        part.setGuest(string2Part(video.getGuest()));
        filmography.setMate(part);
        return filmography;
    }

    /**
     * filmography转化为video
     * @param filmography
     * @return
     */
    public static Video toVideo(Filmography filmography){
        if (null == filmography){
            return null;
        }
        Video video = new Video();
        video.setDescription(filmography.getDesc());
        video.setTitle(filmography.getName());
        video.setYear(filmography.getYear());
        video.setDocid(filmography.getId());
        video.setPlatform(filmography.getPlatform());
        video.setImg(filmography.getImg());
        video.setTag(filmography.getTag());
        video.setDisplayMode(filmography.getDisplayMode());
        ParticipationMate part = filmography.getMate();
        if (null != part){
            video.setActor(part2String(part.getActor()));
            video.setDirector(part2String(part.getDirector()));
            video.setGuest(part2String(part.getGuest()));
        }
        return video;
    }

    /**
     * FilmographyInfo 转化成 VideoInfo
     * @param filmography
     * @return
     */
    public static List<VideoInfo> toListInfo(Filmography filmography){
        List<VideoInfo> infos = new ArrayList<>();
        if (null == filmography || null == filmography.getVideoInfos()){
            return infos;
        }
        for (FilmographyInfo filmographyInfo : filmography.getVideoInfos()){
            VideoInfo info = new VideoInfo();
            info.setDocid(filmography.getId());
            info.setPlatform( filmographyInfo.getPlatform());
            info.setVid(filmographyInfo.getId());
            info.setTitle(filmographyInfo.getTitle());
            info.setOrdin(filmographyInfo.getNumber());
            info.setDescription(filmographyInfo.getDesc());
            info.setUrl(URLUtil.urlNoProtocol(filmographyInfo.getUrl()));
            infos.add(info);
        }
        return infos;
    }

    /**
     * Participation List 转化成 String
     * @param participations
     * @return
     */
    public static String part2String(List<Participation> participations){
        String part = "";
        if (CollectionsUtil.isEmpty(participations)){
            return part;
        }
        for (Participation participation : participations){
            part += "," + participation.getName();
        }
        return part.substring(1);
    }

    /**
     * VideoInfo 转化成 FilmographyInfo
     * @param infos
     * @return
     */
    public static List<FilmographyInfo>  episode2VideoInfo(List<VideoInfo> infos){
        List<FilmographyInfo> filmographyInfoList = new ArrayList<>();
        if (CollectionsUtil.isEmpty(infos)){
            return filmographyInfoList;
        }
        int i = 0;
        for (VideoInfo info : infos){
            i ++;
            FilmographyInfo filmographyInfo = new FilmographyInfo();;
            int number = i;
            if (null != info.getOrdin()){
                number = info.getOrdin();
            }
            filmographyInfo.setNumber(number);
            filmographyInfo.setId(info.getVid());
            filmographyInfo.setPlatform(info.getPlatform());
            filmographyInfo.setUrl(info.getUrl());
            filmographyInfo.setTitle(info.getTitle());
            filmographyInfo.setDesc(info.getDescription());
            filmographyInfoList.add(filmographyInfo);
        }
        return filmographyInfoList;
    }

    /**
     * string 转换成 Participation list
     * @param participations
     * @return
     */
    public static List<Participation> string2Part(String participations){
        List<Participation> participationList = new ArrayList<>();
        if (StringUtil.isEmpty(participations)){
            return participationList;
        }
        String[] participationsArray = participations.split(",");
        participationList = CollectionsUtil.convert(Arrays.asList(participationsArray),
                accept -> {
                    Participation participation = new Participation();
                    participation.setName(accept);
                    return participation;
                }
        );
        return participationList;
    }

    /**
     * 获取filmography 的 docid
     * @param filmography
     * @return
     */
    public static String parseDocid(Filmography filmography){
        if (PlatformSupport.isSupport(filmography.getPlatform())){
            String[] arr = parse(filmography.getPlatform(), filmography.getUrl());
            if (arr.length == 0){
                return filmography.getId();
            }
            return arr[0];
        }
        // 暂不支持的平台使用MD5值替代docid,
        return EncryptUtil.md5Hex(filmography.getUrl());
    }

    /**
     * 获取docid 和 tvid
     * @param platform
     * @param url
     * @return
     */
    public static String[] parse(String platform, String url){
        if (null == url || url.isEmpty()){
            return new String[]{};
        }
        if (platform.equals(PlatformSupport.PLATFORM_QQ)){
            String[] paths = url.split("/");
            String tvid = paths[paths.length-1];
            tvid = tvid.substring(0, tvid.indexOf("."));
            String docid = paths[paths.length-2];
            if (docid.equals("cover")){
                docid = tvid;
            }
            return new String[]{docid, tvid};
        }
        if (platform.equals(PlatformSupport.PLATFORM_IMGO)){
            String[] paths = url.split("/");
            String tvid = paths[paths.length-1];
            tvid = tvid.substring(0, tvid.indexOf("."));
            String docid = paths[paths.length-2];
            return new String[]{docid, tvid};
        }
        if (platform.equals(PlatformSupport.PLATFORM_YOUKU)){
            String[] paths = url.split("/");
            String tvid = paths[paths.length-1];
            tvid = tvid.substring(3, tvid.indexOf("."));
            // 优酷视频从url无法获取docid,故使用MD5值替代
            return new String[]{EncryptUtil.md5Hex(tvid), tvid};
        }
        if (platform.equals(PlatformSupport.PLATFORM_BILIBILI)){
            String[] paths = url.split("/");
            String tvid = paths[paths.length-1];
            int index = tvid.indexOf("?");
            if (index != -1){
                tvid = tvid.substring(0, index);
            }
            // 哔哩哔哩docid与tvid相同
            return new String[]{tvid, tvid};
        }
        if (platform.equals(PlatformSupport.PLATFORM_LOCAL)){
            return new String[]{url, url};
        }
        if (platform.equals(PlatformSupport.PLATFORM_NETFLY)) {
            String p = url.substring(url.lastIndexOf("/") + 1, url.length() - 5);
            String[] array = p.split("-");
            return new String[]{array[0], array[2]};
        }
        return new String[]{};
    }
}
